<?php

namespace app\store\controller\group;

use app\store\controller\BaseController;
use app\store\model\GroupGoods;
use app\store\service\GoodsService;
use app\store\service\GroupGoodsService;
use think\facade\View;
use think\exception\ValidateException;

class GoodsController extends BaseController
{
    public function index()
    {   
        $param = $this->request->param();


        $groupGoodsService = new GroupGoodsService();
        $list = $groupGoodsService->getGroupGoodsList($param);


        return View::fetch('',['list' => $list]);
    }


    /**
     * 拼团商品选项
     * */
    public function option()
    {
        $param = $this->request->param();



        $groupGoodsService = new GroupGoodsService();
        $list = $groupGoodsService->getGroupGoodsOptions($param);

        View::engine()->layout(false);

        if (!empty($param['group_activity_id'])){

            return View::fetch('',['list' => $list, 'group_activity_id' => $param['group_activity_id']]);
        }


        return View::fetch('',['list' => $list]);
    }


    
    public function create()
    {

        $id = $this->request->get('id/d',0);


        if (!$id){
            return View::fetch('');
        }

        $goodsService = new GoodsService();
        $row = $goodsService->findGoods($id);

        return View::fetch('',['row' => $row]);
    }

    public function edit()
    {   
        $id = $this->request->get('id/d');



        $groupGoodsService = new GroupGoodsService();
        $row =  $groupGoodsService->findGroupGoods($id);

        return View::fetch('',['row' => $row]);
    }

    public function save()
    {
        $param = $this->request->post();

        try {
            validate('GroupGoodsValidate')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $groupGoodsService = new GroupGoodsService();
        $id =  $groupGoodsService->saveGroupGoods($param);
        
        return $id ? json(['code' => 1, '保存成功!']) : json(['code' => 0, '保存失败!']);

    }


    public function switch()
    {
        $param = $this->request->post();

        try {
            $this->validate($param,[
                'id' => 'require|integer',
                'status' => 'require|in:0,1',
            ]);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        GroupGoods::update($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }


    public function update()
    {
        $param = $this->request->post();

        try {
            validate('GroupGoodsValidate.edit')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $groupGoodsService = new GroupGoodsService();
        $id =  $groupGoodsService->updateGroupGoods($param);
        
        return $id ? json(['code' => 1, '保存成功!']) : json(['code' => 0, '保存失败!']);
    }


    public function delete()
    {
        $id = $this->request->get('id/d');
        $result = GroupGoods::destroy($id);

        return $result ?  json(['code' => 1, 'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败!']);
    }
}